package com.xnew.configuration;

import com.xnew.utils.DataOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author yangcd
 * @Date 2020/10/14
 */
@RestController
public class ErrorControllerConfiguration implements ErrorController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final static String ERROR_PATH = "/error";

    @ResponseBody
    @RequestMapping(path  = ERROR_PATH )
    public DataOutput error(HttpServletRequest request, HttpServletResponse response){
        logger.info("ErrorControllerConfiguration error.....");
        DataOutput output = DataOutput.fail();
        output.setCode(response.getStatus());
        output.setMsg(HttpStatus.valueOf(output.getCode()).getReasonPhrase());
        return output;
    }

    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

}
